using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using IWS.Lib.SingleSignOnServer;
using System.Web.Security;
using IWS.Lib.SingleSignOnServer.ServiceResponses;

namespace IWS.Lib.SingleSignOnServer.WebServices
{
	/// <summary>
	/// Summary description for MembershipService
	/// </summary>
	[WebService(Namespace = "http://www.integratedwebsystems.com/Services/", Description = "This is the membership web service portal.")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	public class MembershipService : System.Web.Services.WebService
	{

		public MembershipService()
		{
			//Uncomment the following line if using designed components 
			//InitializeComponent(); 
		}

		protected virtual void LogException(Exception ex)
		{
			//TODO:	override this using a custom logging tool
		}

		private bool ValidateServiceKey(string sServiceKey)
		{
			IWS.Lib.SingleSignOnServer.Service svc = new IWS.Lib.SingleSignOnServer.Service(sServiceKey);
			if (svc != null && svc.ExpirationDate >= DateTime.Now.ToUniversalTime())
			{
				svc.LastActiveDate = DateTime.Now.ToUniversalTime();
				svc.Save();
				return true;
			}

			return false;
		}

		/// <summary>
		/// Get the application name
		/// </summary>
		/// <param name="sSessionKey">Valid session key</param>
		/// <returns></returns>
		[WebMethod(Description = "Get the application name in the membership context")]
		public ServiceResponse GetApplicationName(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.ApplicationName;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}
		/// <summary>
		/// Set an application name. 
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		[WebMethod(Description = "Set the application name in the membership context.")]
		public ServiceResponse SetApplicationName(string sSessionKey, string name)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					Membership.Provider.ApplicationName = name;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}
		/// <summary>
		/// Change a user password
		/// </summary>
		/// <param name="sSessionKey">Session key for application</param>
		/// <param name="username"></param>
		/// <param name="oldPassword">Existing password</param>
		/// <param name="newPassword">Repacement password</param>
		/// <returns></returns>
		[WebMethod(Description = "Change a user password")]
		public ServiceResponse ChangePassword(string sSessionKey, string username, string oldPassword, string newPassword)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.ChangePassword(username, oldPassword, newPassword);
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Change a user's password question and answer
		/// </summary>
		/// <param name="sSessionKey">Session key for application</param>
		/// <param name="username"></param>
		/// <param name="password"></param>
		/// <param name="newPasswordQuestion"></param>
		/// <param name="newPasswordAnswer"></param>
		/// <returns></returns>
		[WebMethod(Description = "Change a user's password question and answer")]
		public ServiceResponse ChangePasswordQuestionAndAnswer(string sSessionKey, string username, string password, string newPasswordQuestion, string newPasswordAnswer)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Create a user account
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="username"></param>
		/// <param name="password"></param>
		/// <param name="email"></param>
		/// <param name="passwordQuestion"></param>
		/// <param name="passwordAnswer"></param>
		/// <param name="isApproved"></param>
		/// <param name="providerUserKey"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		[WebMethod(Description = "Create a user account")]
		public CreateUserServiceResponse CreateUser(string sSessionKey, string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey)
		{
			CreateUserServiceResponse resp = new CreateUserServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					CreateUserResult result = new CreateUserResult();
					result.User = new MembershipUserProxy(Membership.Provider.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out result.CreateStatus));
					resp.Data = result;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Delete a user account.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="username"></param>
		/// <param name="deleteAllRelatedData"></param>
		/// <returns></returns>
		[WebMethod(Description = "Delete a user account.")]
		public ServiceResponse DeleteUser(string sSessionKey, string username, bool deleteAllRelatedData)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.DeleteUser(username, deleteAllRelatedData);
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		///  Gets a value indicating whether the current membership provider allows for password resetting. 
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets a value indicating whether the current membership provider allows for password resetting.")]
		public ServiceResponse GetEnablePasswordReset(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.EnablePasswordReset;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets a value indicating if the membership provider allows retreval of passwords
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets a value indicating if the membership provider allows retreval of passwords")]
		public ServiceResponse GetEnablePasswordRetrieval(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.EnablePasswordRetrieval;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets a collection of membership users, in a page of data, where the e-mail
		/// address contains the specified e-mail address to match.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="emailToMatch"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets a collection of membership users, in a page of data, where the e-mail address contains the specified e-mail address to match.")]
		public UserCollectionServiceResponse FindUsersByEmail1(string sSessionKey, string emailToMatch, int pageIndex, int pageSize)
		{
			UserCollectionServiceResponse resp = new UserCollectionServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new UserCollectionResult();
					resp.Data.SetUserCollection(Membership.Provider.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out resp.Data.TotalRecords));
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets a collection of membership users, in a page of data, where the e-mail address contains the specified e-mail address to match.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="emailToMatch"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets a collection of membership users, in a page of data, where the e-mail address contains the specified e-mail address to match.")]
		public UserCollectionServiceResponse FindUsersByEmail2(string sSessionKey, string emailToMatch)
		{
			UserCollectionServiceResponse resp = new UserCollectionServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new UserCollectionResult();
					resp.Data.SetUserCollection(Membership.FindUsersByEmail(emailToMatch));
					resp.Data.TotalRecords = resp.Data.MatchingUsers.Length;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets a collection of membership users, in a page of data, where the user name contains the specified user name to match.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="usernameToMatch"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets a collection of membership users, in a page of data, where the user name contains the specified user name to match.")]
		public UserCollectionServiceResponse FindUsersByName1(string sSessionKey, string usernameToMatch, int pageIndex, int pageSize)
		{
			UserCollectionServiceResponse resp = new UserCollectionServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new UserCollectionResult();
					resp.Data.SetUserCollection(Membership.Provider.FindUsersByName(usernameToMatch, pageIndex, pageSize, out resp.Data.TotalRecords));
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}
		/// <summary>
		/// Gets a collection of membership users, in a page of data, where the user name contains the specified user name to match.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="usernameToMatch"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets a collection of membership users, in a page of data, where the user name contains the specified user name to match.")]
		public UserCollectionServiceResponse FindUsersByName2(string sSessionKey, string usernameToMatch)
		{
			UserCollectionServiceResponse resp = new UserCollectionServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new UserCollectionResult();
					resp.Data.SetUserCollection(Membership.FindUsersByName(usernameToMatch));
					resp.Data.TotalRecords = resp.Data.MatchingUsers.Length;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets a collection of all the users in the database in pages of data.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="totalRecords"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets a collection of all the users in the database in pages of data.")]
		public UserCollectionServiceResponse GetAllUsers1(string sSessionKey, int pageIndex, int pageSize)
		{
			//TODO: broken!  can't serialize MembershipUserCollection.  CAN serialize MembershipUser.... get to it. 
			UserCollectionServiceResponse resp = new UserCollectionServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new UserCollectionResult();
					resp.Data.SetUserCollection(Membership.Provider.GetAllUsers(pageIndex, pageSize, out resp.Data.TotalRecords));
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets a collection of all the users in the database in pages of data.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets a collection of all the users in the database in pages of data.")]
		public UserCollectionServiceResponse GetAllUsers2(string sSessionKey)
		{
			UserCollectionServiceResponse resp = new UserCollectionServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new UserCollectionResult();
					resp.Data.SetUserCollection(Membership.GetAllUsers());
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets the number of users currently accessing an application.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets the number of users currently accessing an application.")]
		public ServiceResponse GetNumberOfUsersOnline(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.GetNumberOfUsersOnline();
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets the password for the membership user from the membership data store.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="username"></param>
		/// <param name="answer"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets the password for the membership user from the membership data store.")]
		public ServiceResponse GetPassword(string sSessionKey, string username, string answer)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.GetPassword(username, answer);
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets information from the data source for a user. 
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="username"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets information from the data source for a user.")]
		public MembershipUserServiceResponse GetUser1(string sSessionKey, string username)
		{
			MembershipUserServiceResponse resp = new MembershipUserServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new MembershipUserProxy(Membership.GetUser(username));
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets information from the data source for a user. Provides an option to update the last-activity date/time stamp for the user.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="username"></param>
		/// <param name="userIsOnline"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets information from the data source for a user. Provides an option to update the last-activity date/time stamp for the user.")]
		public MembershipUserServiceResponse GetUser2(string sSessionKey, string username, bool userIsOnline)
		{
			MembershipUserServiceResponse resp = new MembershipUserServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new MembershipUserProxy(Membership.Provider.GetUser(username, userIsOnline));
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		/// Gets the information from the data source for the membership user associated with the specified unique identifier.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="UserID"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets the information from the data source for the membership user associated with the specified unique identifier.")]
		public MembershipUserServiceResponse GetUser3(string sSessionKey, object UserID)
		{
			MembershipUserServiceResponse resp = new MembershipUserServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new MembershipUserProxy(Membership.GetUser(UserID));
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		/// <summary>
		///  Gets the information from the data source for the membership user associated with the specified unique identifier. Updates the last-activity date/time stamp for the user, if specified.
		/// </summary>
		/// <param name="sSessionKey"></param>
		/// <param name="UserID"></param>
		/// <param name="userIsOnline"></param>
		/// <returns></returns>
		[WebMethod(Description = "Gets the information from the data source for the membership user associated with the specified unique identifier. Updates the last-activity date/time stamp for the user, if specified.")]
		public MembershipUserServiceResponse GetUser4(string sSessionKey, object UserID, bool userIsOnline)
		{
			MembershipUserServiceResponse resp = new MembershipUserServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = new MembershipUserProxy(Membership.GetUser(UserID, userIsOnline));
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetUserNameByEmail(string sSessionKey, string email)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.GetUserNameByEmail(email);
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetMaxInvalidPasswordAttempts(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.MaxInvalidPasswordAttempts;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetMinRequiredNonAlphanumericCharacters(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.MinRequiredNonAlphanumericCharacters;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetMinRequiredPasswordLength(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.MinRequiredPasswordLength;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetPasswordAttemptWindow(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.PasswordAttemptWindow;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse PasswordFormat(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = System.Web.Security.Membership.Provider.PasswordFormat;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetPasswordStrengthRegularExpression(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.PasswordStrengthRegularExpression;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetRequiresQuestionAndAnswer(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.RequiresQuestionAndAnswer;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetRequiresUniqueEmail(string sSessionKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.RequiresUniqueEmail;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse ResetPassword(string sSessionKey, string username, string answer)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.ResetPassword(username, answer);
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse UnlockUser(string sSessionKey, string userName)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.UnlockUser(userName);
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse UpdateUser(string sSessionKey, MembershipUserProxy user)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					Membership.Provider.UpdateUser(user.GetMembershipUser());
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse ValidateUser(string sSessionKey, string username, string password)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sSessionKey))
				{
					resp.Data = Membership.Provider.ValidateUser(username, password);
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponse.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GenerateSession(string username, string password)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (Membership.Provider.ValidateUser(username, password))
				{
					IWS.Lib.SingleSignOnServer.Service svc = new IWS.Lib.SingleSignOnServer.Service();
					svc.Id = Utility.GetRandomUnicodeString(50);
					svc.ExpirationDate = DateTime.UtcNow.AddDays(1);
					svc.CreateDate = DateTime.UtcNow;
					svc.Save();

					resp.Data = svc.Id;
					resp.Result = ServiceResponse.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponse.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public void ExpireSession(string sServiceKey)
		{
			try
			{
				IWS.Lib.SingleSignOnServer.Service.Destroy(sServiceKey);
			}
			catch (Exception ex)
			{
				LogException(ex);
			}
		}
	}
}